/**
 * 
 */
package br.com.lazyprogrammer.worldsoccermanager.network.task;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;

import br.com.lazyprogrammer.worldsoccermanager.network.JSONConstants;
import br.com.lazyprogrammer.worldsoccermanager.network.Request;
import br.com.lazyprogrammer.worldsoccermanager.network.URLParams;
import br.com.lazyprogrammer.worldsoccermanager.network.builders.URLBuilder;
import br.com.lazyprogrammer.worldsoccermanager.network.interfaces.LoginListener;
import br.com.lazyprogrammer.worldsoccermanager.system.message.Message;
import br.com.lazyprogrammer.worldsoccermanager.util.Parser;

/**
 * 
 * @author Limao Games <br />
 * <b>Email:</b> andreikeda@limaogames.com.br <br />
 * created on: 08/05/2014
 * @version 1.0
 * @see 
 */
public class LoginTask extends AsyncTask<String, Void, Request.Response> {
	
	private Request.Callback callback;
	private Context context;
	private LoginListener listener;
	
	public LoginTask(Context context, Request.Callback callback, LoginListener listener) {
		this.context	= context;
		this.callback	= callback;
		this.listener	= listener;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		callback.onStart(getClass());
	}

	/* (non-Javadoc)
	 * @see android.os.AsyncTask#doInBackground(Params[])
	 */
	@Override
	protected Request.Response doInBackground(String... params) {
		// TODO Auto-generated method stub
		String username	= params[0];
		String password	= params[1];
		Parser parser	= new Parser();
        Request.Response status;
		try {
			JSONObject parent	= new Request().post(new URLBuilder().buildUrlSystem(), new URLParams().buildLoginParams(username, password));
			status	= Request.Response.values()[parser.parseResponse(parent)];
			if (status.equals(Request.Response.Success))
				listener.onLoginSuccessful(parser.parseUser(parent.getJSONObject(JSONConstants.OBJECT).getJSONObject(JSONConstants.USER)), password);
		} catch (SocketTimeoutException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.SocketTimeout;
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.UnsupportedEncoding;
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.ClientProtocol;
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.IllegalState;
			e.printStackTrace();
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.NullPointer;
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.IO;
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.JSON;
			e.printStackTrace();
		}
		return status;
	}
	
	@Override
	protected void onPostExecute(Request.Response result) {
		if (isCancelled())
			callback.onCancel(getClass());
		else {
			if (result.equals(Request.Response.Success))
				callback.onSuccess(getClass(), result);
			else
				callback.onError(getClass(), result, Message.getException(context, result));
		}
	}

}
